package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2024-07-14
 * Time: 14:23
 */
public class AddIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {

        //1.判满
        int currentSize = bookList.getUseSized();
        if(currentSize == bookList.getBooks().length){
            System.out.println("书放满了");
        }

        //2.插入
        System.out.println("新增图书...");
        System.out.println("请输入书名：");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        System.out.println("请输入作者：");
        String autor = sc.nextLine();
        System.out.println("请输入价格：");
        Scanner sc1 = new Scanner(System.in);
        int price = sc1.nextInt();
        System.out.println("请输入类型：");
        String type = sc.nextLine();

        Book newbook = new Book(name,autor,price,type);

        //3.判断书架有没有这本书
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("书架里有这本书,不能插入");
                return;
            }
        }

        //4.插入这本书
        bookList.setBook(currentSize,newbook);
        bookList.setUseSized(currentSize + 1);
        System.out.println("新增图书成功");
    }
}
